<?php
namespace Resmodel\Factory;



use Resmodel\ArticleTable;
use Resmodel\CategoryTable;
use Resmodel\DayDishTable;
use ResModel\DishLangTable;
use ResModel\DishTable;
use Resmodel\MlCategoryLangTable;
use Resmodel\MlCategoryTable;
use Resmodel\ResAbstractTable;
use ResModel\UnitLangTable;
use ResModel\UnitTable;
use Zend\Db\Adapter\Adapter;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class ResTableFinder implements ServiceLocatorAwareInterface{

   private $dbAdapter;

   private $domain;

    /**
     * @param mixed $domain
     */
    public function setDomain($domain)
    {
        $this->domain = $domain;
    }

    /**
     * @return mixed
     */
    public function getDomain()
    {
        return $this->domain;
    }

    /**
     * @param mixed $dbAdapter
     */
    public function setDbAdapter($dbAdapter)
    {
        $this->dbAdapter = $dbAdapter;

    }

    /**
     * @return mixed
     */
    public function getDbAdapter()
    {
        if(!$this->dbAdapter){
            return $this->getDefaultAdapter();
        }
        return $this->dbAdapter;
    }

    public function getDefaultAdapter(){
        if(!$this->dbAdapter){
            $dbname = str_replace('-', '_',$this->getDomain());
            $sm = $this->getServiceLocator();
            $config = $sm->get('Config');
            $dbConf = $config['db'];
            $dsn = $dbConf['dsn'];
            $tmpArr = explode(';',$dsn);
            $tmpArr2 = explode('=',$tmpArr[0]);
            $dsn=$tmpArr2[0].'='.$dbname.';'.$tmpArr[1];
            $dbConf['dsn'] = $dsn;
            $adapter = new Adapter($dbConf);
            $this->dbAdapter = $adapter;
        }

        return  $this->dbAdapter;
    }

   private $serviceLocator;

    /**
     * Set service locator
     *
     * @param ServiceLocatorInterface $serviceLocator
     */
    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
    {
        $this->serviceLocator = $serviceLocator;
    }

    /**
     * Get service locator
     *
     * @return ServiceLocatorInterface
     */
    public function getServiceLocator()
    {
        return $this->serviceLocator;
    }

    /**
     * @var CategoryTable
     */
    protected $categoryTable;

    public function getCategoryTable($lang)
    {
        if (!$this->categoryTable) {
            $sm = $this->getServiceLocator();
            $this->categoryTable = $sm->get('Resmodel\CategoryTable');

            $this->categoryTable->setLang($lang);
            $this->categoryTable->setDomain($this->getDomain());
            $this->categoryTable->setAdapter($this->getDbAdapter());
        }
        return $this->categoryTable;
    }

    /**
     * @var MlCategoryTable
     */
    protected $mlCategoryTable;

    /**
     * @param $lang
     * @return MlCategoryTable
     */
    public function getMlCategoryTable($lang)
    {
        if (!$this->mlCategoryTable) {
            $sm = $this->getServiceLocator();
            $this->mlCategoryTable = $sm->get('Resmodel\MlCategoryTable');

            $this->mlCategoryTable->setLang($lang);
            $this->mlCategoryTable->setDomain($this->getDomain());
            $this->mlCategoryTable->setAdapter($this->getDbAdapter());
        }
        return $this->mlCategoryTable;
    }


    /**
     * @var MlCategoryLangTable
     */
    protected $mlCategoryLangTable;

    /**
     * @param $lang
     * @return MlCategoryLangTable
     */
    public function getMlCategoryLangTable($lang)
    {
        if (!$this->mlCategoryLangTable) {
            $sm = $this->getServiceLocator();
            $this->mlCategoryLangTable = $sm->get('Resmodel\MlCategoryLangTable');
            $this->mlCategoryLangTable->setLang($lang);
            $this->mlCategoryLangTable->setDomain($this->getDomain());
            $this->mlCategoryLangTable->setAdapter($this->getDbAdapter());
        }
        return $this->mlCategoryLangTable;
    }

    /**
     * @var ArticleTable
     */
    protected $articleTable;

    public function getArticleTable()
    {
        if (!$this->articleTable) {
            $sm = $this->getServiceLocator();
            $this->articleTable = $sm->get('Resmodel\ArticleTable');
            $this->articleTable->setDomain($this->getDomain());
            $this->articleTable ->setAdapter($this->getDbAdapter());
        }
        return $this->articleTable;
    }

    /**
     * @var DishTable
     */
    protected $dishTable;

    public function getDishTable($lang)
    {
        if (!$this->dishTable) {
            $sm = $this->getServiceLocator();
            $this->dishTable = $sm->get('Resmodel\DishTable');
            $this->dishTable->setDomain($this->getDomain());
            $this->dishTable->setLang($lang);
            $this->dishTable ->setAdapter($this->getDbAdapter());
        }
        return $this->dishTable;
    }

    /**
     * @var DishLangTable
     */
    protected $dishLangTable;

    public function getDishLangTable($lang)
    {
        if (!$this->dishLangTable) {
            $sm = $this->getServiceLocator();
            $this->dishLangTable = $sm->get('Resmodel\DishLangTable');
            $this->dishLangTable->setDomain($this->getDomain());
            $this->dishLangTable->setLang($lang);
            $this->dishLangTable ->setAdapter($this->getDbAdapter());
        }
        return $this->dishLangTable;
    }

    /**
     * @var UnitTable
     */
    protected $unitTable;
    public function getUnitTable($lang)
    {
        if (!$this->unitTable) {
            $sm = $this->getServiceLocator();
            $this->unitTable = $sm->get('Resmodel\UnitTable');

            $this->unitTable->setLang($lang);
            $this->unitTable->setDomain($this->getDomain());
            $this->unitTable->setAdapter($this->getDbAdapter());
        }
        return $this->unitTable;
    }

    /**
     * @var UnitLangTable
     */
    protected $unitLangTable;

    public function getUnitLangTable()
    {
        if (!$this->unitLangTable) {
            $sm = $this->getServiceLocator();
            $this->unitLangTable = $sm->get('Resmodel\UnitLangTable');
            $this->unitLangTable->setDomain($this->getDomain());
            $this->unitLangTable ->setAdapter($this->getDbAdapter());
        }
        return $this->unitLangTable;
    }

    /**
     * @var DayDishTable
     */
    protected $dayDishTable;

    public function getDayDishTable()
    {
        if (!$this->dayDishTable) {
            $sm = $this->getServiceLocator();
            $this->dayDishTable = $sm->get('Resmodel\DayDishTable');
            $this->dayDishTable->setDomain($this->getDomain());
            $this->dayDishTable ->setAdapter($this->getDbAdapter());
        }
        return $this->dayDishTable;
    }

    /**
     * @param ResAbstractTable $table
     * @param string $domain
     */
    /*private function injectAdapter($table){
        $table->setDomain($this->getDomain());
        $table->setAdapter($this->getDbAdapter());
    }*/


}